//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.whuims.pois.action;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.core.helpers.Strings;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.action.result.NewsResult;
import org.whuims.pois.dao.NewsDao;

public class NewsAction extends DispatchAction {
    private NewsDao newsDao = new NewsDao();

    public NewsAction() {
    }

    public ActionForward newest(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String type = request.getParameter("type");
        if(type == null) {
            type = "news";
        }

        long topicId = this.getTopic(request);
        String userName = (String)request.getSession().getAttribute("userName");
        if(topicId <= 0L) {
            return mapping.findForward("login");
        } else {
            List list = this.newsDao.getNewest(type, topicId, userName);
            request.setAttribute("result", list);
            String tag = "最新新闻";
            if(type.equalsIgnoreCase("bbs")) {
                tag = "论坛热帖";
            } else if(type.equalsIgnoreCase("blog")) {
                tag = "热门博文";
            } else if(type.equalsIgnoreCase("weibo")) {
                tag = "微博热议";
            }

            request.setAttribute("tag", tag);
            request.setAttribute("type", type);
            request.setAttribute("isWeixinBlog", "no");
            return mapping.findForward("success");
        }
    }

    public ActionForward newestWithLanguage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String type = request.getParameter("type");
        String language = request.getParameter("language");
        if(type == null) {
            type = "news";
        }

        long topicId = this.getTopic(request);
        String userName = (String)request.getSession().getAttribute("userName");
        if(topicId <= 0L) {
            return mapping.findForward("login");
        } else {
            List list = this.newsDao.getNewestWithLanguage(type, topicId, userName, language);
            request.setAttribute("result", list);
            String tag = "最新新闻";
            if(type.equalsIgnoreCase("bbs")) {
                tag = "论坛热帖";
            } else if(type.equalsIgnoreCase("blog")) {
                tag = "热门博文";
            } else if(type.equalsIgnoreCase("weibo")) {
                tag = "微博热议";
            }

            request.setAttribute("tag", tag);
            request.setAttribute("type", type);
            request.setAttribute("isWeixinBlog", "no");
            return mapping.findForward("success");
        }
    }

    public ActionForward weixinBlog(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String userName = (String)request.getSession().getAttribute("userName");
        long topicId = ((Long)request.getSession().getAttribute("topicId")).longValue();
        List result = this.newsDao.getWeixinBlogs(topicId, 8L, userName);
        if(result != null && result.size() > 0) {
            request.setAttribute("result", result);
            request.setAttribute("tag", "微信公众号");
            request.setAttribute("isWeixinBlog", "yes");
            return mapping.findForward("success");
        } else {
            request.setAttribute("tag", "微信公众号");
            return mapping.findForward("success");
        }
    }

    public ActionForward count(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String language = request.getParameter("language");
        String startDate = request.getParameter("sd");
        String endDate = request.getParameter("ed");
        Long topic = Long.valueOf(this.getTopic(request));
        if(topic.longValue() <= 0L) {
            return mapping.findForward("login");
        } else {
            String type = request.getParameter("type");
            if(Strings.isEmpty(type)) {
                type = "news";
            }

            if(language == null || language.trim().equals("") || language.trim().equals("null")) {
                language = null;
            }

            int count = this.newsDao.getNewsCount(type, topic, language, startDate, endDate);
            request.setAttribute("count", Integer.valueOf(count));
            request.setAttribute("language", language);
            request.setAttribute("startDate", startDate);
            request.setAttribute("endDate", endDate);
            request.setAttribute("topicId", topic);
            request.setAttribute("type", type);
            request.setAttribute("isWeixinBlog", "no");
            return mapping.findForward("success");
        }
    }

    public ActionForward all(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String startStr = request.getParameter("start");
        String pageSizeStr = request.getParameter("pagesize");
        Long topicId = Long.valueOf(this.getTopic(request));
        if(topicId.longValue() <= 0L) {
            return mapping.findForward("login");
        } else {
            String type = request.getParameter("type");
            String language = request.getParameter("language");
            String startDate = request.getParameter("sd");
            String endDate = request.getParameter("ed");
            if(startStr == null || startStr.trim().equals("")) {
                startStr = "0";
            }

            if(pageSizeStr == null || pageSizeStr.trim().equals("")) {
                pageSizeStr = "10";
            }

            if(language == null || language.trim().equals("") || language.trim().equals("null")) {
                language = "all";
            }

            System.out.println(startStr + "\t" + pageSizeStr + "\t" + type + "\t" + language);

            int start;
            try {
                start = Integer.parseInt(startStr);
            } catch (NumberFormatException var22) {
                start = 1;
            }

            if(start <= 0) {
                start = 1;
            }

            int pageSize;
            try {
                pageSize = Integer.parseInt(pageSizeStr);
            } catch (NumberFormatException var21) {
                pageSize = 10;
            }

            if(pageSize > 50 || pageSize < 1) {
                pageSize = 10;
            }

            try {
                response.setCharacterEncoding("utf-8");
                PrintWriter e = response.getWriter();
                int count = this.newsDao.getNewsCount(type, topicId, language, startDate, endDate);
                request.setAttribute("count", Integer.valueOf(count));
                List list = this.newsDao.getNews(type, topicId, language, startDate, endDate, start, pageSize);
                Gson gson = new Gson();
                NewsResult result = new NewsResult();
                result.setResult(list);
                result.setCount(count);
                String json = gson.toJson(result);
                e.write(json);
                e.close();
            } catch (IOException var20) {
                var20.printStackTrace();
            }

            return null;
        }
    }

    public ActionForward negative(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        long topicId = this.getTopic(request);
        if(topicId < 0L) {
            return mapping.findForward("login");
        } else {
            String userName = (String)request.getSession().getAttribute("userName");
            String type = "news";
            List list = this.newsDao.getNegative(userName, type, Long.valueOf(topicId));
            request.setAttribute("result", list);
            request.setAttribute("tag", "负面新闻");
            request.setAttribute("isWeixinBlog", "no");
            return mapping.findForward("success");
        }
    }

    public ActionForward similar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        long topicid = this.getTopic(request);
        if(topicid <= 0L) {
            return mapping.findForward("login");
        } else {
            long id;
            try {
                id = Long.parseLong(request.getParameter("similarId"));
            } catch (NumberFormatException var10) {
                request.setAttribute("result", new ArrayList());
                return mapping.findForward("similar");
            }

            List list = this.newsDao.getSimilar(topicid, id);
            request.setAttribute("cluster", this.newsDao.getCluster(id));
            request.setAttribute("result", list);
            request.setAttribute("tag", "相似新闻");
            request.setAttribute("isWeixinBlog", "no");
            return mapping.findForward("similar");
        }
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        return topic == null?-1L:((Long)topic).longValue();
    }
}
